var express = require('express');
var router = express.Router();
const classService = require('../service/classService');

router.get('/all',async function(req,res){
     let data = await classService.findAll();
     res.send(data);
});

router.get('/',async function(req,res){
     let {type,value,current,pageSize} = req.query;
     let searchParams = {};
     if(type){
         if(type == "count"){
             searchParams[type] = value;
         }else{
             searchParams[type] = {$regex:value};
         }
     }
     let data = await classService.find({current:current || 1,pageSize:pageSize || 5},searchParams);
     res.send(data);
 });

 /**
 * @swagger
 * /classes/index/{id}:
 *    put:
 *      summary: 修改排序
 *      tags:
 *       - classes
 *      parameters:
 *      - name: id
 *        in: path
 *        description: 班级id
 *        required: true
 *      requestBody:
 *        required: true
 *        content:
 *          application/x-www-form-urlencoded:
 *            schema:
 *              type: object
 *              properties:
 *                index:          
 *                  type: integer
 *                incOrDec:    
 *                  type: integer
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
 router.put('/index/:id',async function(req,res){
    let classId = req.params.id;
    let {index,incOrDec} = req.body;
    let data = await classService.updateIndex(classId,index,incOrDec);
    res.send(data);
});

module.exports = router;